<?php
if (!defined('INWEB')) header("Location: ../index.php");
$a=getVar('a');
$id=getVar('id');
includeLang('creasons');
switch($a)
{
    case 'add':
    if($_POST)
    {
        try
        {
            $name=getVar('name');

            $sql->query("INSERT INTO `canceled_reasons` (`name`) VALUES ('$name');");
            msg($Lang['success'],$Lang['creason_added'].'</br> <a href="?p='.$page.'">'.$Lang['back'].'</a>');
            
        }
        catch(Exception $e)
        {
            msg($Lang['error'],$e->getMessage(),'error');
        }
    }
    else
    {
        echo '<form action="?p='.$page.'&a=add" method="post"><table border="1">';
        echo '<tr><th>'.$Lang['name'].'</th><td><input size="64" type="text" name="name" /></td></tr>';
        echo'</table><input type="submit" class="yt_button" value="'.$Lang['add'].'" /></form>';
    }
    break;
    case 'edit':
        if($_POST)
        {
            try
            {
                $name=getVar('name');
                $sql->query("UPDATE `canceled_reasons` SET `name`='$name' WHERE id='$id';");
                msg($Lang['success'],$Lang['creason_updated'].'</br> <a href="?p='.$page.'">'.$Lang['back'].'</a>');
                
            }
            catch(Exception $e)
            {
                msg($Lang['error'],$e->getMessage(),'error');
            }
        }
        else
        {
            $sql->query("SELECT * FROM `canceled_reasons` WHERE `id`='$id';");
            $crsn=$sql->fetch_array();
            echo '<form action="?p='.$page.'&a=edit&id='.$id.'" method="post"><table border="1">';
            echo '<tr><th>'.$Lang['name'].'</th><td><input size="64" type="text" name="name" value="'.$crsn['name'].'" /></td></tr>';
            echo'</table><input type="submit" class="yt_button" value="'.$Lang['edit'].'" /></form>';
        }
    break;
    case 'delete':
        if(isset($_GET['confirm']))
        {
            $sql->query("DELETE FROM `canceled_reasons` WHERE `id`='$id';");
            msg($Lang['success'],$Lang['creason_deleted']);
        }
        else
        {
            msg($Lang['warning'],$Lang['are_u_sure_delete_creason'].$id.' ?<br /> <a href="?p='.$page.'&a=delete&id='.$id.'&confirm">'.$Lang['yes'].'</a>/<a href="?p='.$page.'">'.$Lang['no'].'</a>','warning');
        }
    break;
    default:
        echo '<a href="?p='.$page.'&a=add"><img src="img/add.png" alt="'.$Lang['add'].'" title="'.$Lang['add'].'" /></a><table border="1">';
        echo '<tr><td>'.$Lang['name'].'</td><td>'.$Lang['actions'].'</td></tr>';

        $creasons=$sql->query("SELECT `id`, `name` FROM `canceled_reasons`;");
        while($crsn=$sql->fetch_array($creasons))
        {
            echo '<tr><td>'.$crsn['name'].'</td><td><a href="?p='.$page.'&a=edit&id='.$crsn['id'].'"><img src="img/edit.png" alt="'.$Lang['edit'].'" title="'.$Lang['edit'].'" /></a><a href="?p='.$page.'&a=delete&id='.$crsn['id'].'"><img src="img/delete.png" alt="'.$Lang['delete'].'" title="'.$Lang['delete'].'" /></a></td></tr>';
        }
        echo '</table>';
    break;
}
?>